package com.zy.clothing.action;

import com.zy.clothing.domain.ClothesDb;
import com.zy.clothing.domain.ClothingDb;
import com.zy.clothing.domain.UserGao;
import com.zy.clothing.domain.UserTb;
import com.zy.clothing.service.ClothesDbService;
import com.zy.clothing.service.ClothingDbService;
import com.zy.clothing.service.UserGaoService;
import com.zy.clothing.service.UserTbService;
import com.zy.clothing.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: clothing
 * @description 描述
 * @author: 周佳佳 用户控制层
 * @create: 2020-03-10 22:00
 **/
@Controller
@RequestMapping("UserController")
public class UserTbController {
    @Resource
    UserTbService userTbService;

    @Resource
    ClothingDbService clothingDbService;

    @Resource
    ClothesDbService clothesDbService;

    @Resource
    UserGaoService userGaoService;


    @RequestMapping("findGonggao")
    @ResponseBody
    public List<UserGao> findGonggao(){
        List<UserGao> userGao=userGaoService.selectByPrimaryKey();
        return userGao;
    }

    @RequestMapping("findById")
    @ResponseBody
    public UserTb findById(UserTb userTbt, HttpSession session){
        UserTb userTb=null;
        if(!StringUtils.isEmpty(userTbt)){
            userTb=userTbService.selectByPrimaryKey(userTbt);
            session.setAttribute("user",userTb);
        }
        return userTb;
    }
    @RequestMapping("addUser")
    @ResponseBody
    public Message addUser(UserTb userTb){
        userTb.setUserPermissions(0);
       int rows=userTbService.insertSelective(userTb);
       Message message=new Message();
       if(rows>0){
           message.setMsg("注册成功!");
           message.setNum(1);
       }
        return message;
    }
    @RequestMapping("findByName")
    @ResponseBody
    public Message findByName(UserTb userName){
        int rows=userTbService.countAll(userName);
        Message message=new Message();
        if(rows>0){
            message.setMsg("用户名已存在请换一个!");
            message.setNum(1);
        }
        return message;
    }
    @RequestMapping("findByUserAndName")
    @ResponseBody
    public Message findByUserAndName(UserTb userTb){
        int rows=userTbService.countAll(userTb);
        Message message=new Message();
        if(rows>0){
            message.setMsg("登录成功!");
            message.setNum(1);
        }else{
            message.setMsg("用户名密码不正确");
            message.setNum(0);
        }
        return message;
    }
    /**
     * 查询是否已经登录
     * @param session
     * @return
     */
    @RequestMapping("/findModByManager")
    @ResponseBody
    public UserTb findModByManager(HttpSession session) {

        UserTb man = (UserTb) session.getAttribute("user");
        if(man==null){
            man = new UserTb();
            man.setUsId(0);
            man.setUserName("请重新登录");
        }
        return man;
    }
    /**
     * 退出系统清除session
     * @param session
     * @return
     */
    @RequestMapping("/removeSession")
    @ResponseBody
    public Message removeSession(HttpSession session) {
        session.invalidate();
        Message msg  = new Message();
        msg.setMsg("成功");
        msg.setNum(1);
        return msg;
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public Map<String,Object> selectAll(ClothingDb clothingDb,@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        List<ClothingDb> list=clothingDbService.selectByPrimaryKey(clothingDb,pageNum,pageSize);
        int count=clothingDbService.countAll(clothingDb);
        map.put("data", list);
        map.put("count", count);
        map.put("code", 0);
        map.put("msg", "");//消息
        return map;
    }
    @RequestMapping("selectType")
    @ResponseBody
    public List<ClothingDb> selectType(ClothingDb clothingDb){
        List<ClothingDb> list=clothingDbService.selectType(clothingDb);
        return list;
    }
    @RequestMapping("yiwuadd")
    @ResponseBody
    public Message yiwuadd(ClothingDb clothingDb){
        int rows=clothingDbService.insertSelective(clothingDb);
        Message message=new Message();
        if(rows>0){
            message.setMsg("增加类型成功!");
            message.setNum(1);
        }
        return message;
    }

    @RequestMapping("insertHuishou")
    @ResponseBody
    public Message insertHuishou(ClothesDb clothingDb, HttpServletRequest request){
        clothingDb.setCoIsva(1);
        clothingDb.setVoPerson("root");
        Message message=new Message();
        int rows=clothesDbService.insertSelective(clothingDb);
        if(rows>0){
            ClothingDb clothingDb1=clothingDbService.findById(clothingDb.getClId());

            UserTb userTb= (UserTb) request.getSession().getAttribute("user");
            UserTb userTb1=userTbService.findById(userTb.getUsId());

            //得到所得积分
            int fen=clothingDb1.getClJifen()*clothingDb.getCoNum();
            if(userTb1.getUserIntegral()>0){
                UserTb userTb2=new UserTb();
                userTb2.setUsId(userTb1.getUsId());
                userTb2.setUserIntegral(fen+userTb1.getUserIntegral());
                int rowss=userTbService.updateByPrimaryKeySelective(userTb2);
            }else{
                UserTb userTb3=new UserTb();
                userTb3.setUsId(userTb1.getUsId());
                userTb3.setUserIntegral(fen);
                int rowsss=userTbService.updateByPrimaryKeySelective(userTb3);
            }
            message.setMsg("增加类型成功!");
            message.setNum(1);
        }
        return message;
    }
    @RequestMapping("selectUserAll")
    @ResponseBody
    public Map<String,Object> selectUserAll(){

        Map<String,Object> map=new HashMap<>();

        List<UserTb> list=userTbService.selectAllone();
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @RequestMapping("selectHuishouAll")
    @ResponseBody
    public Map<String,Object> selectHuishouAll(ClothesDb clothesDb){
        Map<String,Object> map=new HashMap<>();
        List<ClothesDb> list=clothesDbService.selectByPrimaryKey(clothesDb);
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Message delete(Integer clId){
        Message message=new Message();
        int rows=clothingDbService.deleteByPrimaryKey(clId);
        if(rows>0){
            message.setMsg("删除成功!");
            message.setNum(1);
        }
        return message;
    }

    @RequestMapping("insertGongyi")
    @ResponseBody
    public Message insertGongyi(UserGao userGao,HttpServletRequest request){
        UserTb userTb= (UserTb) request.getSession().getAttribute("user");
        userGao.setGaoName(userTb.getUserName());
        userGao.setUsId(userTb.getUsId());
        Message message=new Message();
        int rows=userGaoService.insertSelective(userGao);
        if(rows>0){
            message.setMsg("增加!");
            message.setNum(1);
        }
        return message;
    }
}
